﻿using System;
using System.Collections;
using System.Text;
using System.Xml;

namespace MPMovieCatalog.Domain
{
    [Serializable()]
    public class MovieInfo : NamedEntity, IComparable<MovieInfo>
    {
        private const string DEFAULT_SEPARATOR = "; ";

        private ArrayList m_genres = new ArrayList();

        private ArrayList m_actors = new ArrayList();

        private ArrayList m_files = new ArrayList();

        public void AddActor(Actor actor)
        {
            if (m_actors == null)
            {
                m_actors = new ArrayList();
            }
            m_actors.Add(actor);
        }

        public void AddGenre(Genre genre)
        {
            if (m_genres == null)
            {
                m_genres = new ArrayList();
            }
            m_genres.Add(genre);
        }

        public void AddFile(ShareFile file)
        {
            if (m_files == null)
            {
                m_files = new ArrayList();
            }
            m_files.Add(file);
        }

        public XmlDocument ToXML()
        {
            XmlDocument movieXML = new XmlDocument();
            XmlElement root = movieXML.CreateElement("movieInfo");
            movieXML.AppendChild(root);

            ToXML(root);
            return movieXML;
        }

        public void ToXML(XmlElement root)
        {
            XmlDocument document = root.OwnerDocument;

            XmlElement element = document.CreateElement("id");
            element.AppendChild(document.CreateTextNode(this.Id.ToString()));
            root.AppendChild(element);

            element = document.CreateElement("title");
            element.AppendChild(document.CreateTextNode(this.Name));
            root.AppendChild(element);

            element = document.CreateElement("tagline");
            element.AppendChild(document.CreateTextNode(this.Tagline));
            root.AppendChild(element);

            element = document.CreateElement("description");
            element.AppendChild(document.CreateTextNode(this.Description));
            root.AppendChild(element);

            element = document.CreateElement("imdbRating");
            element.AppendChild(document.CreateTextNode(this.IMDBRating.Replace(',', '.')));
            root.AppendChild(element);

            element = document.CreateElement("imdbNumber");
            element.AppendChild(document.CreateTextNode(this.IMDBNumber));
            root.AppendChild(element);

            element = document.CreateElement("imdbVotes");
            element.AppendChild(document.CreateTextNode(this.IMDBVotes));
            root.AppendChild(element);

            element = document.CreateElement("year");
            element.AppendChild(document.CreateTextNode(this.Year));
            root.AppendChild(element);

            element = document.CreateElement("runtime");
            element.AppendChild(document.CreateTextNode(this.Runtime.ToString()));
            root.AppendChild(element);

            element = document.CreateElement("mpaaRating");
            element.AppendChild(document.CreateTextNode(this.MPAARating));
            root.AppendChild(element);

            element = document.CreateElement("director");
            element.AppendChild(document.CreateTextNode(this.Director));
            root.AppendChild(element);

            element = document.CreateElement("writer");
            element.AppendChild(document.CreateTextNode(this.Writer));
            root.AppendChild(element);

            element = document.CreateElement("picture");
            element.AppendChild(document.CreateTextNode(this.PictureFileName));
            root.AppendChild(element);

            element = document.CreateElement("isWatched");
            element.AppendChild(document.CreateTextNode(this.IsWatched.ToString().ToLower()));
            root.AppendChild(element);

            element = document.CreateElement("discId");
            element.AppendChild(document.CreateTextNode(this.DiscId));
            root.AppendChild(element);

            element = document.CreateElement("actors");
            root.AppendChild(element);
            foreach (Actor actor in m_actors)
            {
                XmlElement child = document.CreateElement("actor");
                child.AppendChild(document.CreateTextNode(actor.Name));
                element.AppendChild(child);
            }

            element = document.CreateElement("genres");
            root.AppendChild(element);
            foreach (Genre genre in m_genres)
            {
                XmlElement child = document.CreateElement("genre");
                child.AppendChild(document.CreateTextNode(genre.Name));
                element.AppendChild(child);
            }

            element = document.CreateElement("files");
            root.AppendChild(element);
            foreach (ShareFile file in m_files)
            {
                XmlElement child = document.CreateElement("file");
                child.AppendChild(document.CreateTextNode(file.Name));
                element.AppendChild(child);
            }

            if ((m_files != null) && (m_files.Count > 0))
            {
                ShareFile file = m_files[0] as ShareFile;
                if (!string.IsNullOrEmpty(file.MediaInfo))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(file.MediaInfo);

                    root.AppendChild(document.ImportNode(doc.DocumentElement, true));

                    /*
                    element = document.CreateElement("mediaInfo");
                    root.AppendChild(element);

                    element.InnerXml = doc.DocumentElement.InnerXml;
                     */
                }
            }
        }

        public string Title
        {
            get
            {
                return Name;
            }

            set
            {
                Name = value;
            }
        }

        public string Picture { get; set; }

        public string PictureFileName { get; set; }

        public string Thumb { get; set; }

        public string Tagline { get; set; }

        public string Description { get; set; }

        public string IMDBRating { get; set; }

        public string IMDBNumber { get; set; }

        public string IMDBVotes { get; set; }

        public string Year { get; set; }

        public int Runtime { get; set; }

        public string MPAARating { get; set; }

        public string Director { get; set; }

        public string Writer { get; set; }

        public bool IsWatched { get; set; }

        public string DiscId { get; set; }
        
        public ArrayList Genres
        {
            get { return m_genres; }
            set { m_genres = value; }
        }

        public ArrayList Actors
        {
            get { return m_actors; }
            set { m_actors = value; }
        }

        public ArrayList Files
        {
            get { return m_files; }
            set { m_files = value; }
        }

        public string ActorsAsString
        {
            get
            {
                return ActorsToString(DEFAULT_SEPARATOR);
            }
        }

        public string ActorsToString(string separator)
        {
            return ListToString(Actors, separator);
        }

        public string GenresAsString
        {
            get
            {
                return GenresToString(DEFAULT_SEPARATOR);
            }
        }

        public string GenresToString(string separator)
        {
            return ListToString(Genres, separator);
        }

        public string FilesAsString
        {
            get
            {
                return FilesToString(DEFAULT_SEPARATOR);
            }
        }

        public string FilesToString(string separator)
        {
            return ListToString(Files, separator);
        }

        private string ListToString(ArrayList list, string separator)
        {
            StringBuilder sb = new StringBuilder();
            foreach (NamedEntity entity in list)
            {
                if (sb.Length > 0)
                {
                    sb.Append(separator);
                }
                sb.Append(entity.Name);
            }
            return sb.ToString();
        }

        #region IComparable<MovieShortInfo> Members

        public int CompareTo(MovieInfo other)
        {
            return base.CompareTo(other);
        }

        #endregion
    }
}
